package com.recorder.myapplication.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2019/5/12.
 */

public class KeepManager {

    private static final KeepManager mInstance = new KeepManager();

    private KeepReceiver mKeepReceiver;

    private WeakReference<Activity> mKeepActivity;

    private KeepManager() {
    }

    public static KeepManager getmInstance() {
        return mInstance;
    }

    public void registerKeep(Context context) {

        IntentFilter intentFilter = new IntentFilter();

        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);

        KeepReceiver keepReceiver = new KeepReceiver();

        context.registerReceiver(keepReceiver, intentFilter);

    }

    //注销
    public void unRegisterKeep(Context context) {
        if (mKeepReceiver != null) {
            context.unregisterReceiver(mKeepReceiver);
        }
    }

    public void startKeep(Context context) {
        Intent intent = new Intent(context, KeepActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public void finishKeep() {
        System.out.println("准备销毁了  ");

        if (mKeepActivity != null) {
            Activity activity = mKeepActivity.get();
            if (activity != null) {
                System.out.println("销毁了 1 1 1 1  1 1 1  1 1 1 1  ");
                activity.finish();
            }
            mKeepActivity = null;
        }

    }


    public void setKeep(KeepActivity keep) {
        mKeepActivity = new WeakReference<Activity>(keep);
    }

}
